

#include "xapp_config.h"

#if USER_CHECK_ENABLED( TOUCHPANEL )

#if USER_CHECK_ENABLED( TOUCHPANEL) == 0 
#error "System TPDFU enable, but xapp_touchpanel is DISABLE\r\n"
#endif // !USER_CHECK_ENABLED( TOUCHPANEL) == 0 

#include "system_list.h"

#include "xapp_osllv.h"
#include "xapp_system.h"
#include "xapp_touchpanel.h"
#include "xapp_system.h"

#include "em_rtc.h"

#include "xtask+peripheral.h"

#include "log.h"
#define LOG     LOG_INFO0


static void init(void)
{    
    LOG("[SYSTEM]: enter TPDFU mode"); 
    
    OSLLV_SEND_TAG_EXE( OSLLV_TAG_TPDFU );
    xapp_touchpanel_disable();
    xapp_touchpanel_start_upgrade();

    xapp_system_timer_start_single_shot( 1000 );
}

static void destroy(void)
{
    LOG("[SYSTEM]: exit TPDFU mode"); 
    xapp_touchpanel_enable();
    xapp_system_timer_stop();
}

static void task_loop(uint8_t flag)
{
    const xapp_touchpanel_desc_t* p_tp_desc = xapp_touch_panel_get_desc();
    
    if( p_tp_desc->stage != XAPP_TOUCHPANEL_STAGE_UPGRAGE )
    {
        xapp_system_set_mode( SYSTEM_MODE_NORMAL );
    }
    else 
    {
        xapp_system_timer_start_single_shot( 1000 );
    }
}

const user_system_desc_t  gsSYSTEM_TPDFU = 
{
    .mode = SYSTEM_MODE_TPDFU,
    .function = 
    {
    .init = init,
    .destroy = destroy,
    .task_loop = task_loop
    }
};

#endif // !USER_CHECK_ENABLED( TOUCHPANEL )
